package com.ztgf.shopping.api.cms.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import com.ztgf.shopping.models.service.epg.IEpgAggregationBannerService;
import com.ztgf.shopping.models.bo.epg.EpgAggregationBannerSaveBO;
import com.ztgf.shopping.models.bo.epg.EpgAggregationBannerBO;
import com.ztgf.shopping.models.dto.epg.EpgAggregationBannerDTO;

/**
 * 聚合页banner控制器
 *
 * @author YUE
 * @Date 2020-06-04 15:36:46
 */

@RestController
@RequestMapping("/cms/epgAggregationBanner")
@Slf4j
@Api(value = "EpgAggregationBannerController",tags = "聚合页banner接口")
public class EpgAggregationBannerController {

    @DubboReference
    private IEpgAggregationBannerService epgAggregationBannerService;

    /**
     * 频道栏目列表 根据功能id
     * @param moduleId 功能id
     * @return
     */
    @ApiOperation(value = "频道栏目列表 根据功能id",notes = "频道栏目列表 根据功能id")
    @PostMapping("/getListByModuleId")
    public Result<List<EpgAggregationBannerDTO>> getListByModuleId(Long moduleId){
        EpgAggregationBannerBO epgAggregationBannerBO = new EpgAggregationBannerBO();
        epgAggregationBannerBO.setModuleId(moduleId);
        List<EpgAggregationBannerDTO> epgAggregationBannerDTOList = epgAggregationBannerService.getBySelectBannerInfoNoPage(epgAggregationBannerBO);
        return new Result<>(epgAggregationBannerDTOList);
    }

    @ApiOperation(value = "添加聚合页banner",notes = "添加聚合页banner")
    @PostMapping(value = "/insert")
    public Result insert(EpgAggregationBannerSaveBO saveBO) {
        boolean flag = epgAggregationBannerService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存聚合页banner",notes = "保存聚合页banner")
    public Result save(EpgAggregationBannerSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改聚合页banner",notes = "修改聚合页banner")
    @PostMapping("/update")
    public Result update(EpgAggregationBannerSaveBO saveBO){
        EpgAggregationBannerDTO dto = new EpgAggregationBannerDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = epgAggregationBannerService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态",notes = "修改状态")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
             ,@ApiImplicitParam(name = "status",value = "状态 0正常、1删除",required = true,paramType = "query", dataType = "Integer")
     })
     public Result updateStatus(Long id, Integer status){
         EpgAggregationBannerDTO dto = new EpgAggregationBannerDTO();
         dto.setId(id);
         dto.setStatus(status);
        boolean flag = epgAggregationBannerService.updateById(dto);
         if(flag){
             return new Result<>();
         } else {
             return new Result<>(CodeEnum.FAIL_100008);
         }
     }

    @ApiOperation(value = "删除聚合页banner",notes = "删除聚合页banner")
    @ApiImplicitParam(name = "id",value = "聚合页bannerid",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = epgAggregationBannerService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除聚合页banner",notes = "批量删除聚合页banner")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        epgAggregationBannerService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<EpgAggregationBannerDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        EpgAggregationBannerDTO dto = epgAggregationBannerService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "聚合页banner列表",notes = "聚合页banner列表")
    @PostMapping("/queryPageList")
    public Result<Page<EpgAggregationBannerDTO>> queryPageList(EpgAggregationBannerBO bo){
        Wrapper<EpgAggregationBannerDTO> wrapper = new EntityWrapper<>();

                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }

        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<EpgAggregationBannerDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = epgAggregationBannerService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
